package com.ly.system.controller;

import cn.hutool.core.util.StrUtil;
import com.ly.common.core.bean.R;
import com.ly.common.core.constant.BaseConstant;
import com.ly.common.core.utils.MinioUtil;
import com.ly.system.service.ISysDictService;
import com.ly.system.system.bean.dto.FieldCheckDTO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 公共
 *
 * @author luoyun
 * @Date 2025-05-28
 */
@Slf4j
@RestController
@RequestMapping("/sys/common")
@RequiredArgsConstructor
public class CommonController {

    private final ISysDictService sysDictService;

    private String uploadpath = BaseConstant.upload;
    private String uploadType = BaseConstant.uploadType;

    /**
     * 字段重复校验
     *
     * @return
     */
    @Operation(summary = "字段重复校验")
    @GetMapping("/fieldCheck")
    public R<String> fieldCheck(FieldCheckDTO dto) {
        return sysDictService.duplicateCheckData(dto);
    }

    /**
     * 文件上传统一方法
     *
     * @param request
     * @return
     */
    @PostMapping("/upload")
    public R<String> upload(HttpServletRequest request) throws Exception {
        String savePath = "";
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        if (BaseConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) {
            savePath = this.uploadLocal(file);
        } else {
            savePath = MinioUtil.uploadFile(file);
        }
        if (StrUtil.isEmpty(savePath)) {
            return R.fail("上传失败！");
        }
        return R.ok(savePath);
    }

    /**
     * 本地文件上传
     *
     * @param mf 文件
     * @return
     */
    private String uploadLocal(MultipartFile mf) {
        try {
            String ctxPath = uploadpath;
            String fileName = null;
            File file = new File(ctxPath + File.separator + File.separator);
            if (!file.exists()) {
                file.mkdirs();
            }
            // 获取文件名
            String orgName = mf.getOriginalFilename();
            if (orgName.indexOf(".") != -1) {
                fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
            } else {
                fileName = orgName + "_" + System.currentTimeMillis();
            }
            String savePath = file.getPath() + File.separator + fileName;
            File savefile = new File(savePath);
            FileCopyUtils.copy(mf.getBytes(), savefile);
            return savePath;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }
}
